package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.SysUserRoleService;
import com.atguigu.spzx.model.pojo.system.SysRole;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.utils.DYLAssert;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Tag(name = "用户角色对应接口")
@RequestMapping("/user/system/sysUserRole")
@CrossOrigin(allowCredentials = "true", originPatterns = "*", allowedHeaders = "*")
public class SysUserRoleController {
    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Operation(summary = "根据用户id查询角色")
    @GetMapping("/findRolesByUserId/{id}")
    public Result<List<SysRole>> findRolesByUserId(@PathVariable("id") Long id) {
        DYLAssert.notNull(id, "查询失败，用户id为空");
        List<SysRole> lists = sysUserRoleService.getRolesByUserId(id);
        return Result.ok(lists);
    }

    @Operation(summary = "分配角色")
    @PostMapping("/assignUserRole/{userId}")
    public Result assignUserRole(@PathVariable("userId") Long userId, @RequestBody List<Long> roleIds){
        DYLAssert.notNull(roleIds,"分配失败，角色id为空");
        DYLAssert.notNull(userId,"分配失败，用户id为空");
        DYLAssert.isTrue(sysUserRoleService.assignUserRole(userId,roleIds),"分配失败");
        return Result.ok();
    }
}
